package de.fmaul.common.io;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import com.google.common.base.Predicate;

public class StringContentMatcher implements Predicate<File> {

	private final String text;
	
	public StringContentMatcher(String text) {
		this.text = text;
	}
	
	public boolean apply(File input) throws RuntimeException {
		BufferedReader reader = null;
		try {
			reader = new BufferedReader(new FileReader(input));
			String line;
			while ((line = reader.readLine()) != null)   {
				if (line.contains(text)) {
					return true;
				}
			}
			
		} catch (FileNotFoundException e) {
			throw new RuntimeException(e);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
		finally {
			try {
				reader.close();
			} catch (IOException e) {
				throw new RuntimeException("Error closing Reader.", e);
			}
		}
		
		return false;
	}

}
